package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.adorkable.iosdialog.BottomSheetDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.bean.EventBean;
import com.konne.nightmare.FastPublicOpinion.bean.MessageEvent;
import com.konne.nightmare.FastPublicOpinion.http.BaseResponse;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.EventPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IEventFragmentView;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.SheetDialog;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddCoursewareAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddDataAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddDatabaseAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddDiscussAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddExamAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddJobAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddMeetingAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.ExamAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.MembersManagerActivity;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.EventAdapter;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.utils.MMKVUtils;
import com.konne.nightmare.FastPublicOpinion.utils.ToastUtil;
import com.konne.nightmare.FastPublicOpinion.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;


public class EventFragment extends BaseMvpFragment<IEventFragmentView, EventPresenter> implements IEventFragmentView {
    @BindView(R.id.iv_right)
    ImageView iv_right;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.rv_event)//selectEventPlanList
    RecyclerView rv_event;
/*    @BindView(R.id.srf_event_refresh)
    SmartRefreshLayout srf_event_refresh;*/
    private PopupWindow popupWindow;
    private EventAdapter eventAdapter;
    @BindView(R.id.event_layout)
    LinearLayout event_layout;
    int eventId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public int getLayout() {
        return R.layout.event_fragment;
    }

    @Override
    public void initView() {
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.drawable.home4);
        iv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("成员管理",0,0))
                        .addSheet(new SheetDialog.Sheet("添加资料库",0,1))
                        .addSheet(new SheetDialog.Sheet("添加课件",0,2))
                        .addSheet(new SheetDialog.Sheet("添加资料",0,3))
                        .addSheet(new SheetDialog.Sheet("添加作业",0,4))
                        .addSheet(new SheetDialog.Sheet("添加考试",0,5))
                        .addSheet(new SheetDialog.Sheet("添加会议",0,6))
                        .addSheet(new SheetDialog.Sheet("添加讨论",0,7))
                        .addSheet(new SheetDialog.Sheet("考试前可编辑",0,8))
                        .addSheet(new SheetDialog.Sheet("考试结束不可编辑",0,9))

                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                            switch (sheet.action){
                                case 0:
                                    MembersManagerActivity.start();
                                    break;
                                case 1:
                                    AddDatabaseAct.start();
                                    break;
                                case 2:
                                    AddCoursewareAct.start();
                                    break;
                                case 3:
                                    AddDataAct.start();
                                    break;
                                case 4:
                                    AddJobAct.start();
                                    break;
                                case 5:
                                    AddExamAct.start();
                                    break;
                                case 6:
                                    AddMeetingAct.start();
                                    break;
                                case 7:
                                    AddDiscussAct.start();
                                    break;
                                case 8:
                                    ExamAct.start(0);
                                    break;
                                case 9:
                                    ExamAct.start(1);
                                    break;
                            }
                            }
                        }).show();
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }


    /**
     * 提供Fragment实例
     *
     * @return
     */
    public static EventFragment newInstance() {
        EventFragment fragment = new EventFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRedPointStateChange(MessageEvent e) {
        if (null == e) {
            return;
        }

    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return dialog;
    }

    @Override
    public void cancelLoadDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    @Override
    protected EventPresenter createPresenter() {
        return new EventPresenter();
    }



    @Override
    public void selectEventPlanList(BaseResponse<List<EventBean.RowsBean>> data) {
        List<EventBean.RowsBean> rowsBeans = data.getRows();
        eventAdapter.setNewData(rowsBeans);

    }

    @Override
    public void updateEventPlan(BaseResponse<String> data) {
        if (data.getCode() == 200) {
            ToastUtil.showToast(data.getData());
            mPresenter.selectEventPlanList();
        }

    }

    @Override
    public int agencyId() {
        int mmkvInteger = MMKVUtils.getMMKVInteger(Utils.BELONGID);
        return mmkvInteger;
    }

    @Override
    public int eventId() {
        return eventId;
    }

    @Override
    public void delete(BaseResponse<String> data) {
        if (data.getCode() == 200) {
            popupWindow.dismiss();
            mPresenter.selectEventPlanList();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.selectEventPlanList();
    }
}
